package org.smr.ministore.micro;

import java.util.List;

import org.smr.ministore.dao.AuthorityMapper;
import org.smr.ministore.dao.RoleMapper;
import org.smr.ministore.dao.UserMapper;
import org.smr.ministore.entities.Authority;
import org.smr.ministore.entities.Role;
import org.smr.ministore.entities.User;
import org.smr.ministore.service.UserService;

import org.blade.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
//@ConditionalOnProperty(name = "app.isDistributed", havingValue = "true")
public class UserMicroService implements UserService {

	@Autowired
	private UserMapper mapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AuthorityMapper authorityMapper;

    @ResponseBody
	@RequestMapping(method = RequestMethod.POST, value = "/User/batchSave")
	public int batchSave(@RequestBody List<User> entities) {
    	return mapper.batchInsert(entities);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT, value = "/User/save")
    public int save(@RequestBody User entity) {



    	return mapper.insert(entity);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.PATCH, value = "/User/updateById")
    public int updateById(@RequestBody User id) {
   	 	return mapper.updateById(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/User/queryById/{id}")
    public User queryById( @PathVariable("id")Long id ) {
    	return (User) mapper.selectById(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/User/deleteById/{id}")
    public int deleteById( @PathVariable("id")Long id ) {
        return mapper.deleteById(id);
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.POST, value = "/User/paginate/{pageSize}/{pageNum}")
    public Pager<User> paginate( User entity, @PathVariable("pageSize")Long pageSize,  @PathVariable("pageNum")Long pageNum ) {
        Pager<User> pager = new Pager(pageSize, pageNum);
        List<User> list = this.mapper.pagination( entity, pager );
        pager.setEntities(list);
        return pager;
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.GET, value = "/User/queryByUserName/{userName}")
    public User queryByUserName( @PathVariable("userName")  String userName ) {
        return this.mapper.selectByUserName( userName );
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.GET, value = "/User/getCountByUserName/{userName}")
    public Long getCountByUserName( @PathVariable("userName") String userName ) {
        return this.mapper.getCountByUserName( userName );
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.GET, value = "/User/getRoleByUser/{userId}")
    public List<Role> getRoleByUser( @PathVariable("userId") Long userId ) {
        return this.roleMapper.selectByUser(userId);
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.GET, value = "/User/getAuthoritiesByUser/{userId}")
    public List<Authority> getAuthoritiesByUser( @PathVariable("userId") Long userId ) {
        return authorityMapper.selectByUser(userId);
    }

    @ResponseBody
    @RequestMapping( method = RequestMethod.GET, value = "/User/queryByMobile/{mobilePhone}")
    public User queryByMobile( @PathVariable("mobilePhone")  String mobilePhone ) {
        return this.mapper.selectByMobile( mobilePhone );
    }
}
